package com.ruoyi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.domain.entity.PhaseDictionary;
import com.ruoyi.project.domain.entity.ProjectImplement;
import com.ruoyi.project.domain.entity.ProjectPay;
import com.ruoyi.project.domain.query.ProjectImplementQuery;
import com.ruoyi.project.domain.query.ProjectPayQuery;
import com.ruoyi.project.mapper.ProjectImplementMapper;
import com.ruoyi.project.mapper.ProjectPayMapper;
import com.ruoyi.project.service.IPhaseDictionaryService;
import com.ruoyi.project.service.IProjectImplementService;
import com.ruoyi.project.service.IProjectPayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ProjectImplementServiceImpl extends ServiceImpl<ProjectImplementMapper, ProjectImplement> implements IProjectImplementService {

    @Resource
    private ProjectImplementMapper implementMapper;
    @Resource
    private IPhaseDictionaryService dictionaryService;

    //分页
    @Override
    public TableDataInfo getPage(ProjectImplementQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<ProjectImplement> list = implementMapper.getList(query);
        return new TableDataInfo(list, page.getTotal());
    }

    //新增
    @Override
    public Boolean addInfo(ProjectImplement project) {
        project.setOperator(SecurityUtils.getLoginUser().getUser().getNickName());
        Long phaseId = project.getPhaseId();
        if (phaseId == null) {
            throw new RuntimeException("后端接口异常");
        }
        PhaseDictionary byId = dictionaryService.getById(project.getPhaseId());
        project.setPhaseName(byId.getPhaseName());
        return this.save(project);
    }

    @Override
    public Boolean updateInfo(ProjectImplement project) {
        project.setOperator(SecurityUtils.getLoginUser().getUser().getNickName());
        return this.updateById(project);
    }

    @Override
    public Boolean deleteInfo(String id) {
        return this.removeById(id);
    }

    @Override
    public ProjectImplement getInfoById(String id) {
        return this.getById(id);
    }

    @Override
    public List<ProjectImplement> getList(ProjectImplementQuery query) {
        LambdaQueryWrapper<ProjectImplement> wrapper = new QueryWrapper<ProjectImplement>().lambda()
                .eq(query.getProjectNo()!=null,ProjectImplement::getProjectNo,query.getProjectNo())
                .eq(query.getPhaseId()!=null,ProjectImplement::getPhaseId,query.getPhaseId());
        return this.list(wrapper);
    }
}
